﻿/**
 *@description:
 *@file: text_render.cpp
 *@author: snow-tyan
 *@email: zziywang@163.com
 *@date: 30 January 2023
 *@copyright: 2018-2022 AiRiA. All rights reserved.
 */

#include "mygl/util.h"
#include "mygl/es_image.h"
#include "mygl/shader.h"
#include "mygl/text_renderer.h"

#include <cmath>
#include <iostream>
#include <map>
#include <memory>

#include <glad/glad.h>   // 管理OpenGL的函数指针
#include <GLFW/glfw3.h>  // 提供窗口和OpenGL context

#ifndef PROJECT_ROOT
#define PROJECT_ROOT "/home/snow/OpenGLESProjects/MyOpenGLES_glfw"
#endif

using namespace mygl;

const std::string kProjectRoot = PROJECT_ROOT + std::string("/");
const std::string kWorkSpaceRoot = kProjectRoot + "07-InPractice/01-TextRendering/";
const std::string kShaderRoot = kWorkSpaceRoot + "chinese_render/";
const std::string kResourcesRoot = kProjectRoot + "resources/fonts/";
const unsigned int kWindowWidth = 800;
const unsigned int kWindowHeight = 600;
const char *kWindowName = "ChineseRendering";

void PrintGlmVector(const glm::vec3 &vec) {
  printf("vec(%.1f, %.1f, %.1f)\n", vec.x, vec.y, vec.z);
}

int main() {
  // glfw: initialize and configure
  // ------------------------------
  glfwInit();
  // OpenGL 3.3
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  // 核心模式

  // glfw window creation
  // --------------------
  GLFWwindow *window =
      glfwCreateWindow(kWindowWidth, kWindowHeight, kWindowName, NULL, NULL);
  if (window == NULL) {
    std::cout << "Failed to create GLFW window" << std::endl;
    glfwTerminate();
    return -1;
  }
  glfwMakeContextCurrent(window);

  // glad: load all OpenGL function pointers
  // ---------------------------------------
  if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
    std::cout << "Failed to initialize GLAD" << std::endl;
    return -1;
  }
  // print: gl info
  printf("GL_VENDOR: %s\n", glGetString(GL_VENDOR));  // 打印gl供应商
  printf("GL_VERSION: %s\n", glGetString(GL_VERSION));  // 打印gl版本，确定使用gles3
  // printf("%s\n", glGetString(GL_EXTENSIONS));  // 打印GL_EXTENSIONS
  int max_attributes;
  glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_attributes);  // 硬件支持的最大顶点属性数量
  printf("Max number of vertex attribute supported: %d\n", max_attributes);

  glViewport(0, 0, kWindowWidth, kWindowHeight);

  // Set OpenGL options
  glEnable(GL_CULL_FACE);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  // build and compile our shader zprogram
  // ------------------------------------
  auto shader =
      std::make_shared<Shader>(kShaderRoot + "shader.vs", kShaderRoot + "shader.fs");
  if (shader->Init()) {
    std::cout << "Shader Init Failed!" << std::endl;
    return -1;
  }
  auto cn_renderer = std::make_shared<TextRenderer>(shader, kWindowWidth, kWindowHeight);

  std::wstring cn_text = L"这是一段普普通通的中文!";
  std::wstring cn_text2 = L"我的OpenGL";
  cn_renderer->LoadChinese(cn_text, kResourcesRoot + "simfang.ttf", 48);
  cn_renderer->LoadChinese(cn_text2, kResourcesRoot + "simfang.ttf", 48);

  // Game loop
  while (!glfwWindowShouldClose(window)) {
    // Check and call events
    glfwPollEvents();

    // Clear the colorbuffer
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    cn_renderer->RenderChinese(cn_text, glm::vec2(25.0f, 25.0f),     // position
                               1.0f,                                 // scale
                               glm::vec3(0.5, 0.8f, 0.2f));          // color
    cn_renderer->RenderChinese(cn_text2, glm::vec2(670.0f, 570.0f),  // position
                               0.5f,                                 // scale
                               glm::vec3(0.3, 0.7f, 0.9f));          // color
    // Swap the buffers
    glfwSwapBuffers(window);
  }

  glfwTerminate();
  return 0;
}